export function decodeBase64(base64) {
    const buffer = Buffer.from(base64, 'base64');
    return buffer.toString();
}

export function decodeJwtPayload(jwt) {
    const parts = jwt.split('.');
    return JSON.parse(decodeBase64(parts[1]));
}

export function getCookieObj(cookie) {
    const keyArr = cookie.split(';');
    const cookieObj = {};
    for (let i = 0; i < keyArr.length; i++) {
        const keyValue = keyArr[i].split('=');
        const key = keyValue[0].trim();
        const val = keyValue[1];
        cookieObj[key] = val;
    }
    return cookieObj;
}

export function getSessionKey(cookie) {
    return cookie.split(';')[0];
}

export function getCookieExpires(cookie) {
    return new Date(getCookieObj(cookie).expires);
}
